#include "test4.hpp"
#include "VmModel.hpp"
#include "config.hpp"
#include "rander.hpp"

static auto half_sep = "===================";

static void print_helper(ALGO algo, std::vector<int> const &requests,
                         int page_fault, int n_mem) {
  auto algo_str = algo == OPT ? "OPT " : (algo == LRU ? "LRU " : "FIFO");
  printf("%s\tMISS COUNT: %d\tHIT RATE: %f\n", algo_str, page_fault,
         1 - float(page_fault) / requests.size());
}

void test4_helper(ALGO algo, int request_size, int n_mem) {
  rander rander(0, VM_N_BLOCK - 1);
  std::vector<int> requests(rander.get_rand_vector(request_size));

  VmModel vmModel(PAGE_SIZE, VM_N_BLOCK, n_mem, algo);

  std::vector<std::vector<mem_block>> res;
  auto page_fault = vmModel.simulate(requests, res);
  print_helper(algo, requests, page_fault, n_mem);
}

void test4() {

  for (int i = MIN_N_MEM; i <= MAX_N_MEM; i += STEP_N_MEM) {
    printf("%s TEST %d %s\nvmsize = %d  pagesize = %d  memcount = %d\n",
           half_sep, (i - MIN_N_MEM) / STEP_N_MEM, half_sep,
           VM_N_BLOCK * PAGE_SIZE, PAGE_SIZE, i);
    test4_helper(OPT, TEST_SIZE, i);
    test4_helper(LRU, TEST_SIZE, i);
    test4_helper(FIFO, TEST_SIZE, i);
  }
}